home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / floatconv.c < prev    next >
C/C++ Source or Header  |  1994-10-13  |  73KB  |  2,358 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. #include <libioP.h>
  26. #ifdef _IO_USE_DTOA
  27. /****************************************************************
  28.  *
  29.  * The author of this software is David M. Gay.
  30.  *
  31.  * Copyright (c) 1991 by AT&T.
  32.  *
  33.  * Permission to use, copy, modify, and distribute this software for any
  34.  * purpose without fee is hereby granted, provided that this entire notice
  35.  * is included in all copies of any software which is or includes a copy
  36.  * or modification of this software and in all copies of the supporting
  37.  * documentation for such software.
  38.  *
  39.  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  40.  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
  41.  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  42.  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  43.  *
  44.  ***************************************************************/
  45.  
  46. /* Some cleaning up by Per Bothner, bothner@cygnus.com, 1992, 1993.
  47.    Re-written to not need static variables
  48.    (except result, result_k, HIWORD, LOWORD). */
  49.  
  50. /* Please send bug reports to
  51.         David M. Gay
  52.         AT&T Bell Laboratories, Room 2C-463
  53.         600 Mountain Avenue
  54.         Murray Hill, NJ 07974-2070
  55.         U.S.A.
  56.         dmg@research.att.com or research!dmg
  57.  */
  58.  
  59. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  60.  *
  61.  * This strtod returns a nearest machine number to the input decimal
  62.  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
  63.  * broken by the IEEE round-even rule.  Otherwise ties are broken by
  64.  * biased rounding (add half and chop).
  65.  *
  66.  * Inspired loosely by William D. Clinger's paper "How to Read Floating
  67.  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  68.  *
  69.  * Modifications:
  70.  *
  71.  *      1. We only require IEEE, IBM, or VAX double-precision
  72.  *              arithmetic (not IEEE double-extended).
  73.  *      2. We get by with floating-point arithmetic in a case that
  74.  *              Clinger missed -- when we're computing d * 10^n
  75.  *              for a small integer d and the integer n is not too
  76.  *              much larger than 22 (the maximum integer k for which
  77.  *              we can represent 10^k exactly), we may be able to
  78.  *              compute (d*10^k) * 10^(e-k) with just one roundoff.
  79.  *      3. Rather than a bit-at-a-time adjustment of the binary
  80.  *              result in the hard case, we use floating-point
  81.  *              arithmetic to determine the adjustment to within
  82.  *              one bit; only in really hard cases do we need to
  83.  *              compute a second residual.
  84.  *      4. Because of 3., we don't need a large table of powers of 10
  85.  *              for ten-to-e (just some small tables, e.g. of 10^k
  86.  *              for 0 <= k <= 22).
  87.  */
  88.  
  89. /*
  90.  * #define IEEE_8087 for IEEE-arithmetic machines where the least
  91.  *      significant byte has the lowest address.
  92.  * #define IEEE_MC68k for IEEE-arithmetic machines where the most
  93.  *      significant byte has the lowest address.
  94.  * #define Sudden_Underflow for IEEE-format machines without gradual
  95.  *      underflow (i.e., that flush to zero on underflow).
  96.  * #define IBM for IBM mainframe-style floating-point arithmetic.
  97.  * #define VAX for VAX-style floating-point arithmetic.
  98.  * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
  99.  * #define No_leftright to omit left-right logic in fast floating-point
  100.  *      computation of dtoa.
  101.  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
  102.  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
  103.  *      that use extended-precision instructions to compute rounded
  104.  *      products and quotients) with IBM.
  105.  * #define ROUND_BIASED for IEEE-format with biased rounding.
  106.  * #define Inaccurate_Divide for IEEE-format with correctly rounded
  107.  *      products but inaccurate quotients, e.g., for Intel i860.
  108.  * #define KR_headers for old-style C function headers.
  109.  */
  110.  
  111. #ifdef DEBUG
  112. #include <stdio.h>
  113. #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
  114. #endif
  115.  
  116. #ifdef __STDC__
  117. #include <stdlib.h>
  118. #include <string.h>
  119. #include <float.h>
  120. #define CONST const
  121. #else
  122. #define CONST
  123. #define KR_headers
  124.  
  125. /* In this case, we assume IEEE floats. */
  126. #define FLT_ROUNDS 1
  127. #define FLT_RADIX 2
  128. #define DBL_MANT_DIG 53
  129. #define DBL_DIG 15
  130. #define DBL_MAX_10_EXP 308
  131. #define DBL_MAX_EXP 1024
  132. #endif
  133.  
  134. #include <errno.h>
  135. #ifndef __MATH_H__
  136. #include <math.h>
  137. #endif
  138.  
  139. #ifdef Unsigned_Shifts
  140. #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
  141. #else
  142. #define Sign_Extend(a,b) /*no-op*/
  143. #endif
  144.  
  145. #if defined(__i386__) || defined(__i860__) || defined(clipper)
  146. #define IEEE_8087
  147. #endif
  148. #if defined(MIPSEL) || defined(__alpha__)
  149. #define IEEE_8087
  150. #endif
  151. #if defined(__sparc__) || defined(sparc) || defined(MIPSEB)
  152. #define IEEE_MC68k
  153. #endif
  154.  
  155. #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
  156.  
  157. #if FLT_RADIX==16
  158. #define IBM
  159. #else
  160. #if DBL_MANT_DIG==56
  161. #define VAX
  162. #else
  163. #if DBL_MANT_DIG==53 && DBL_MAX_10_EXP==308
  164. #define IEEE_Unknown
  165. #else
  166. Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
  167. #endif
  168. #endif
  169. #endif
  170. #endif
  171.  
  172. typedef _G_uint32_t unsigned32;
  173.  
  174. #ifdef IEEE_8087
  175. #define HIWORD 1
  176. #define LOWORD 0
  177. #define TEST_ENDIANNESS  /* nothing */
  178. #else
  179. #if defined(IEEE_MC68k)
  180. #define HIWORD 0
  181. #define LOWORD 1
  182. #define TEST_ENDIANNESS  /* nothing */
  183. #else
  184. static int HIWORD = -1, LOWORD;
  185. static void test_endianness()
  186. {
  187.     union doubleword {
  188.     double d;
  189.     unsigned32 u[2];
  190.     } dw;
  191.     dw.d = 10;
  192.     if (dw.u[0] != 0) /* big-endian */
  193.     HIWORD=0, LOWORD=1;
  194.     else
  195.     HIWORD=1, LOWORD=0;
  196. }
  197. #define TEST_ENDIANNESS  if (HIWORD<0) test_endianness();
  198. #endif
  199. #endif
  200.  
  201. #if 0
  202. union {
  203.   double d;
  204.   unsigned32 x[2];
  205. } _temp;
  206. #endif
  207. #define word0(x) ((unsigned32 *)&x)[HIWORD]
  208. #if 0
  209. #define word0(X) (_temp.d = X, _temp.x[HIWORD])
  210. #define setword0(D,X) (_temp.d = D, _temp.x[HIWORD] = X, D = _temp.d)
  211. #endif
  212. #define word1(x) ((unsigned32 *)&x)[LOWORD]
  213.  
  214. /* The following definition of Storeinc is appropriate for MIPS processors. */
  215. #if defined(IEEE_8087) + defined(VAX)
  216. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  217. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  218. #else
  219. #if defined(IEEE_MC68k)
  220. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  221. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  222. #else
  223. #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  224. #endif
  225. #endif
  226.  
  227. /* #define P DBL_MANT_DIG */
  228. /* Ten_pmax = floor(P*log(2)/log(5)) */
  229. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  230. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  231. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  232.  
  233. #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_Unknown)
  234. #define Exp_shift  20
  235. #define Exp_shift1 20
  236. #define Exp_msk1    0x100000
  237. #define Exp_msk11   0x100000
  238. #define Exp_mask  0x7ff00000
  239. #define P 53
  240. #define Bias 1023
  241. #define IEEE_Arith
  242. #define Emin (-1022)
  243. #define Exp_1  0x3ff00000
  244. #define Exp_11 0x3ff00000
  245. #define Ebits 11
  246. #define Frac_mask  0xfffff
  247. #define Frac_mask1 0xfffff
  248. #define Ten_pmax 22
  249. #define Bletch 0x10
  250. #define Bndry_mask  0xfffff
  251. #define Bndry_mask1 0xfffff
  252. #define LSB 1
  253. #define Sign_bit 0x80000000
  254. #define Log2P 1
  255. #define Tiny0 0
  256. #define Tiny1 1
  257. #define Quick_max 14
  258. #define Int_max 14
  259. #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
  260. #else
  261. #undef  Sudden_Underflow
  262. #define Sudden_Underflow
  263. #ifdef IBM
  264. #define Exp_shift  24
  265. #define Exp_shift1 24
  266. #define Exp_msk1   0x1000000
  267. #define Exp_msk11  0x1000000
  268. #define Exp_mask  0x7f000000
  269. #define P 14
  270. #define Bias 65
  271. #define Exp_1  0x41000000
  272. #define Exp_11 0x41000000
  273. #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
  274. #define Frac_mask  0xffffff
  275. #define Frac_mask1 0xffffff
  276. #define Bletch 4
  277. #define Ten_pmax 22
  278. #define Bndry_mask  0xefffff
  279. #define Bndry_mask1 0xffffff
  280. #define LSB 1
  281. #define Sign_bit 0x80000000
  282. #define Log2P 4
  283. #define Tiny0 0x100000
  284. #define Tiny1 0
  285. #define Quick_max 14
  286. #define Int_max 15
  287. #else /* VAX */
  288. #define Exp_shift  23
  289. #define Exp_shift1 7
  290. #define Exp_msk1    0x80
  291. #define Exp_msk11   0x800000
  292. #define Exp_mask  0x7f80
  293. #define P 56
  294. #define Bias 129
  295. #define Exp_1  0x40800000
  296. #define Exp_11 0x4080
  297. #define Ebits 8
  298. #define Frac_mask  0x7fffff
  299. #define Frac_mask1 0xffff007f
  300. #define Ten_pmax 24
  301. #define Bletch 2
  302. #define Bndry_mask  0xffff007f
  303. #define Bndry_mask1 0xffff007f
  304. #define LSB 0x10000
  305. #define Sign_bit 0x8000
  306. #define Log2P 1
  307. #define Tiny0 0x80
  308. #define Tiny1 0
  309. #define Quick_max 15
  310. #define Int_max 15
  311. #endif
  312. #endif
  313.  
  314. #ifndef IEEE_Arith
  315. #define ROUND_BIASED
  316. #endif
  317.  
  318. #ifdef RND_PRODQUOT
  319. #define rounded_product(a,b) a = rnd_prod(a, b)
  320. #define rounded_quotient(a,b) a = rnd_quot(a, b)
  321. extern double rnd_prod(double, double), rnd_quot(double, double);
  322. #else
  323. #define rounded_product(a,b) a *= b
  324. #define rounded_quotient(a,b) a /= b
  325. #endif
  326.  
  327. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  328. #define Big1 0xffffffff
  329.  
  330. #define Kmax 15
  331.  
  332. /* (1<<BIGINT_MINIMUM_K) is the minimum number of words to allocate
  333.    in a Bigint.  dtoa usually manages with 1<<2, and has not been
  334.    known to need more than 1<<3.  */
  335.  
  336. #define BIGINT_MINIMUM_K 3
  337.  
  338. struct Bigint {
  339.   struct Bigint *next;
  340.   int k;        /* Parameter given to Balloc(k) */
  341.   int maxwds;        /* Allocated space: equals 1<<k. */
  342.   short on_stack;    /* 1 if stack-allocated. */
  343.   short sign;        /* 0 if value is positive or zero; 1 if negative. */
  344.   int wds;        /* Current length. */
  345.   unsigned32 x[1<<BIGINT_MINIMUM_K]; /* Actually: x[maxwds] */
  346. };
  347.  
  348. #define BIGINT_HEADER_SIZE \
  349.   (sizeof(Bigint) - (1<<BIGINT_MINIMUM_K) * sizeof(unsigned32))
  350.  
  351. typedef struct Bigint Bigint;
  352.  
  353. /* Initialize a stack-allocated Bigint. */
  354.  
  355. static Bigint *
  356. Binit
  357. #ifdef KR_headers
  358.         (v) Bigint *v;
  359. #else
  360.         (Bigint *v)
  361. #endif
  362. {
  363.   v->on_stack = 1;
  364.   v->k = BIGINT_MINIMUM_K;
  365.   v->maxwds = 1 << BIGINT_MINIMUM_K;
  366.   v->sign = v->wds = 0;
  367.   return v;
  368. }
  369.  
  370. /* Allocate a Bigint with '1<<k' big digits. */
  371.  
  372. static Bigint *
  373. Balloc
  374. #ifdef KR_headers
  375.         (k) int k;
  376. #else
  377.         (int k)
  378. #endif
  379. {
  380.   int x;
  381.   Bigint *rv;
  382.  
  383.   if (k < BIGINT_MINIMUM_K)
  384.     k = BIGINT_MINIMUM_K;
  385.  
  386.   x = 1 << k;
  387.   rv = (Bigint *)
  388.     malloc(BIGINT_HEADER_SIZE + x * sizeof(unsigned32));
  389.   rv->k = k;
  390.   rv->maxwds = x;
  391.   rv->sign = rv->wds = 0;
  392.   rv->on_stack = 0;
  393.   return rv;
  394. }
  395.  
  396. static void
  397. Bfree
  398. #ifdef KR_headers
  399.         (v) Bigint *v;
  400. #else
  401.         (Bigint *v)
  402. #endif
  403. {
  404.   if (v && !v->on_stack)
  405.     free (v);
  406. }
  407.  
  408. static void
  409. Bcopy
  410. #ifdef KR_headers
  411.         (x, y) Bigint *x, *y;
  412. #else
  413.         (Bigint *x, Bigint *y)
  414. #endif
  415. {
  416.   register unsigned32 *xp, *yp;
  417.   register int i = y->wds;
  418.   x->sign = y->sign;
  419.   x->wds = i;
  420.   for (xp = x->x, yp = y->x; --i >= 0; )
  421.     *xp++ = *yp++;
  422. }
  423.  
  424. /* Make sure b has room for at least 1<<k big digits. */
  425.  
  426. static Bigint *
  427. Brealloc
  428. #ifdef KR_headers
  429.         (b, k) Bigint *b; int k;
  430. #else
  431.         (Bigint * b, int k)
  432. #endif
  433. {
  434.   if (b == NULL)
  435.     return Balloc(k);
  436.   if (b->k >= k)
  437.     return b;
  438.   else
  439.     {
  440.       Bigint *rv = Balloc (k);
  441.       Bcopy(rv, b);
  442.       Bfree(b);
  443.       return rv;
  444.     }
  445. }
  446.  
  447. /* Return b*m+a.  b is modified.
  448.    Assumption:  0xFFFF*m+a fits in 32 bits. */
  449.  
  450. static Bigint *
  451. multadd
  452. #ifdef KR_headers
  453.         (b, m, a) Bigint *b; int m, a;
  454. #else
  455.         (Bigint *b, int m, int a)
  456. #endif
  457. {
  458.         int i, wds;
  459.         unsigned32 *x, y;
  460.         unsigned32 xi, z;
  461.  
  462.         wds = b->wds;
  463.         x = b->x;
  464.         i = 0;
  465.         do {
  466.                 xi = *x;
  467.                 y = (xi & 0xffff) * m + a;
  468.                 z = (xi >> 16) * m + (y >> 16);
  469.                 a = (int)(z >> 16);
  470.                 *x++ = (z << 16) + (y & 0xffff);
  471.                 }
  472.                 while(++i < wds);
  473.         if (a) {
  474.                 if (wds >= b->maxwds)
  475.                         b = Brealloc(b, b->k+1);
  476.                 b->x[wds++] = a;
  477.                 b->wds = wds;
  478.                 }
  479.         return b;
  480.         }
  481.  
  482. static Bigint *
  483. s2b
  484. #ifdef KR_headers
  485.         (result, s, nd0, nd, y9)
  486.     Bigint *result; CONST char *s; int nd0, nd; unsigned32 y9;
  487. #else
  488.         (Bigint *result, CONST char *s, int nd0, int nd, unsigned32 y9)
  489. #endif
  490. {
  491.   int i, k;
  492.   _G_int32_t x, y;
  493.  
  494.   x = (nd + 8) / 9;
  495.   for(k = 0, y = 1; x > y; y <<= 1, k++) ;
  496.   result = Brealloc(result, k);
  497.   result->x[0] = y9;
  498.   result->wds = 1;
  499.  
  500.   i = 9;
  501.   if (9 < nd0)
  502.     {
  503.       s += 9;
  504.       do
  505.     result = multadd(result, 10, *s++ - '0');
  506.       while (++i < nd0);
  507.       s++;
  508.     }
  509.   else
  510.     s += 10;
  511.   for(; i < nd; i++)
  512.     result = multadd(result, 10, *s++ - '0');
  513.   return result;
  514. }
  515.  
  516. static int
  517. hi0bits
  518. #ifdef KR_headers
  519.         (x) register unsigned32 x;
  520. #else
  521.         (register unsigned32 x)
  522. #endif
  523. {
  524.         register int k = 0;
  525.  
  526.         if (!(x & 0xffff0000)) {
  527.                 k = 16;
  528.                 x <<= 16;
  529.                 }
  530.         if (!(x & 0xff000000)) {
  531.                 k += 8;
  532.                 x <<= 8;
  533.                 }
  534.         if (!(x & 0xf0000000)) {
  535.                 k += 4;
  536.                 x <<= 4;
  537.                 }
  538.         if (!(x & 0xc0000000)) {
  539.                 k += 2;
  540.                 x <<= 2;
  541.                 }
  542.         if (!(x & 0x80000000)) {
  543.                 k++;
  544.                 if (!(x & 0x40000000))
  545.                         return 32;
  546.                 }
  547.         return k;
  548.         }
  549.  
  550. static int
  551. lo0bits
  552. #ifdef KR_headers
  553.         (y) unsigned32 *y;
  554. #else
  555.         (unsigned32 *y)
  556. #endif
  557. {
  558.         register int k;
  559.         register unsigned32 x = *y;
  560.  
  561.         if (x & 7) {
  562.                 if (x & 1)
  563.                         return 0;
  564.                 if (x & 2) {
  565.                         *y = x >> 1;
  566.                         return 1;
  567.                         }
  568.                 *y = x >> 2;
  569.                 return 2;
  570.                 }
  571.         k = 0;
  572.         if (!(x & 0xffff)) {
  573.                 k = 16;
  574.                 x >>= 16;
  575.                 }
  576.         if (!(x & 0xff)) {
  577.                 k += 8;
  578.                 x >>= 8;
  579.                 }
  580.         if (!(x & 0xf)) {
  581.                 k += 4;
  582.                 x >>= 4;
  583.                 }
  584.         if (!(x & 0x3)) {
  585.                 k += 2;
  586.                 x >>= 2;
  587.                 }
  588.         if (!(x & 1)) {
  589.                 k++;
  590.                 x >>= 1;
  591.                 if (!x & 1)
  592.                         return 32;
  593.                 }
  594.         *y = x;
  595.         return k;
  596.         }
  597.  
  598. static Bigint *
  599. i2b
  600. #ifdef KR_headers
  601.         (result, i) Bigint *result; int i;
  602. #else
  603.         (Bigint* result, int i)
  604. #endif
  605. {
  606.   result = Brealloc(result, 1);
  607.   result->x[0] = i;
  608.   result->wds = 1;
  609.   return result;
  610. }
  611.  
  612. /* Do: c = a * b. */
  613.  
  614. static Bigint *
  615. mult
  616. #ifdef KR_headers
  617.         (c, a, b) Bigint *a, *b, *c;
  618. #else
  619.         (Bigint *c, Bigint *a, Bigint *b)
  620. #endif
  621. {
  622.         int k, wa, wb, wc;
  623.         unsigned32 carry, y, z;
  624.         unsigned32 *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  625.         unsigned32 z2;
  626.         if (a->wds < b->wds) {
  627.                 Bigint *tmp = a;
  628.                 a = b;
  629.                 b = tmp;
  630.                 }
  631.         k = a->k;
  632.         wa = a->wds;
  633.         wb = b->wds;
  634.         wc = wa + wb;
  635.         if (wc > a->maxwds)
  636.                 k++;
  637.     c = Brealloc(c, k);
  638.         for(x = c->x, xa = x + wc; x < xa; x++)
  639.                 *x = 0;
  640.         xa = a->x;
  641.         xae = xa + wa;
  642.         xb = b->x;
  643.         xbe = xb + wb;
  644.         xc0 = c->x;
  645.         for(; xb < xbe; xb++, xc0++) {
  646.                 if ((y = *xb & 0xffff)) {
  647.                         x = xa;
  648.                         xc = xc0;
  649.                         carry = 0;
  650.                         do {
  651.                                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  652.                                 carry = z >> 16;
  653.                                 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  654.                                 carry = z2 >> 16;
  655.                                 Storeinc(xc, z2, z);
  656.                                 }
  657.                                 while(x < xae);
  658.                         *xc = carry;
  659.                         }
  660.                 if ((y = *xb >> 16)) {
  661.                         x = xa;
  662.                         xc = xc0;
  663.                         carry = 0;
  664.                         z2 = *xc;
  665.                         do {
  666.                                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  667.                                 carry = z >> 16;
  668.                                 Storeinc(xc, z, z2);
  669.                                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  670.                                 carry = z2 >> 16;
  671.                                 }
  672.                                 while(x < xae);
  673.                         *xc = z2;
  674.                         }
  675.                 }
  676.         for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  677.         c->wds = wc;
  678.         return c;
  679.         }
  680.  
  681. /* Returns b*(5**k).  b is modified. */
  682. /* Re-written by Per Bothner to not need a static list. */
  683.  
  684. static Bigint *
  685. pow5mult
  686. #ifdef KR_headers
  687.         (b, k) Bigint *b; int k;
  688. #else
  689.         (Bigint *b, int k)
  690. #endif
  691. {
  692.   static int p05[6] = { 5, 25, 125, 625, 3125, 15625 };
  693.  
  694.   for (; k > 6; k -= 6)
  695.     b = multadd(b, 15625, 0); /* b *= 5**6 */
  696.   if (k == 0)
  697.     return b;
  698.   else
  699.     return multadd(b, p05[k-1], 0);
  700. }
  701.  
  702. /* Re-written by Per Bothner so shift can be in place. */
  703.  
  704. static Bigint *
  705. lshift
  706. #ifdef KR_headers
  707.     (b, k) Bigint *b; int k;
  708. #else
  709.         (Bigint *b, int k)
  710. #endif
  711. {
  712.   int i;
  713.   unsigned32 *x, *x1, *xe;
  714.   int old_wds = b->wds;
  715.   int n = k >> 5;
  716.   int k1 = b->k;
  717.   int n1 = n + old_wds + 1;
  718.  
  719.   if (k == 0)
  720.     return b;
  721.  
  722.   for(i = b->maxwds; n1 > i; i <<= 1)
  723.     k1++;
  724.   b = Brealloc(b, k1);
  725.  
  726.   xe = b->x; /* Source limit */
  727.   x = xe + old_wds; /* Source pointer */
  728.   x1 = x + n; /* Destination pointer */
  729.   if (k &= 0x1f) {
  730.     int k1 = 32 - k;
  731.     unsigned32 z = *--x;
  732.     if ((*x1 = (z >> k1)) != 0) {
  733.       ++n1;
  734.     }
  735.     while (x > xe) {
  736.       unsigned32 w = *--x;
  737.       *--x1 = (z << k) | (w >> k1);
  738.       z = w;
  739.     }
  740.     *--x1 = z << k;
  741.   }
  742.   else
  743.     do {
  744.       *--x1 = *--x;
  745.     } while(x > xe);
  746.   while (x1 > xe)
  747.     *--x1 = 0;
  748.   b->wds = n1 - 1;
  749.   return b;
  750. }
  751.  
  752. static int
  753. cmp
  754. #ifdef KR_headers
  755.         (a, b) Bigint *a, *b;
  756. #else
  757.         (Bigint *a, Bigint *b)
  758. #endif
  759. {
  760.         unsigned32 *xa, *xa0, *xb, *xb0;
  761.         int i, j;
  762.  
  763.         i = a->wds;
  764.         j = b->wds;
  765. #ifdef DEBUG
  766.         if (i > 1 && !a->x[i-1])
  767.                 Bug("cmp called with a->x[a->wds-1] == 0");
  768.         if (j > 1 && !b->x[j-1])
  769.                 Bug("cmp called with b->x[b->wds-1] == 0");
  770. #endif
  771.         if (i -= j)
  772.                 return i;
  773.         xa0 = a->x;
  774.         xa = xa0 + j;
  775.         xb0 = b->x;
  776.         xb = xb0 + j;
  777.         for(;;) {
  778.                 if (*--xa != *--xb)
  779.                         return *xa < *xb ? -1 : 1;
  780.                 if (xa <= xa0)
  781.                         break;
  782.                 }
  783.         return 0;
  784.         }
  785.  
  786. /* Do: c = a-b. */
  787.  
  788. static Bigint *
  789. diff
  790. #ifdef KR_headers
  791.         (c, a, b) Bigint *c, *a, *b;
  792. #else
  793.         (Bigint *c, Bigint *a, Bigint *b)
  794. #endif
  795. {
  796.         int i, wa, wb;
  797.         _G_int32_t borrow, y; /* We need signed shifts here. */
  798.         unsigned32 *xa, *xae, *xb, *xbe, *xc;
  799.         _G_int32_t z;
  800.  
  801.         i = cmp(a,b);
  802.         if (!i) {
  803.                 c = Brealloc(c, 0);
  804.                 c->wds = 1;
  805.                 c->x[0] = 0;
  806.                 return c;
  807.                 }
  808.         if (i < 0) {
  809.                 Bigint *tmp = a;
  810.                 a = b;
  811.                 b = tmp;
  812.                 i = 1;
  813.                 }
  814.         else
  815.                 i = 0;
  816.         c = Brealloc(c, a->k);
  817.         c->sign = i;
  818.         wa = a->wds;
  819.         xa = a->x;
  820.         xae = xa + wa;
  821.         wb = b->wds;
  822.         xb = b->x;
  823.         xbe = xb + wb;
  824.         xc = c->x;
  825.         borrow = 0;
  826.         do {
  827.                 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
  828.                 borrow = y >> 16;
  829.                 Sign_Extend(borrow, y);
  830.                 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
  831.                 borrow = z >> 16;
  832.                 Sign_Extend(borrow, z);
  833.                 Storeinc(xc, z, y);
  834.                 }
  835.                 while(xb < xbe);
  836.         while(xa < xae) {
  837.                 y = (*xa & 0xffff) + borrow;
  838.                 borrow = y >> 16;
  839.                 Sign_Extend(borrow, y);
  840.                 z = (*xa++ >> 16) + borrow;
  841.                 borrow = z >> 16;
  842.                 Sign_Extend(borrow, z);
  843.                 Storeinc(xc, z, y);
  844.                 }
  845.         while(!*--xc)
  846.                 wa--;
  847.         c->wds = wa;
  848.         return c;
  849.         }
  850.  
  851. static double
  852. ulp
  853. #ifdef KR_headers
  854.         (x) double x;
  855. #else
  856.         (double x)
  857. #endif
  858. {
  859.         register _G_int32_t L;
  860.         double a;
  861.  
  862.         L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  863. #ifndef Sudden_Underflow
  864.         if (L > 0) {
  865. #endif
  866. #ifdef IBM
  867.                 L |= Exp_msk1 >> 4;
  868. #endif
  869.                 word0(a) = L;
  870.                 word1(a) = 0;
  871. #ifndef Sudden_Underflow
  872.                 }
  873.         else {
  874.                 L = -L >> Exp_shift;
  875.                 if (L < Exp_shift) {
  876.                         word0(a) = 0x80000 >> L;
  877.                         word1(a) = 0;
  878.                         }
  879.                 else {
  880.                         word0(a) = 0;
  881.                         L -= Exp_shift;
  882.                         word1(a) = L >= 31 ? 1 : 1 << (31 - L);
  883.                         }
  884.                 }
  885. #endif
  886.         return a;
  887.         }
  888.  
  889. static double
  890. b2d
  891. #ifdef KR_headers
  892.         (a, e) Bigint *a; int *e;
  893. #else
  894.         (Bigint *a, int *e)
  895. #endif
  896. {
  897.         unsigned32 *xa, *xa0, w, y, z;
  898.         int k;
  899.         double d;
  900. #ifdef VAX
  901.         unsigned32 d0, d1;
  902. #else
  903. #define d0 word0(d)
  904. #define d1 word1(d)
  905. #endif
  906.  
  907.         xa0 = a->x;
  908.         xa = xa0 + a->wds;
  909.         y = *--xa;
  910. #ifdef DEBUG
  911.         if (!y) Bug("zero y in b2d");
  912. #endif
  913.         k = hi0bits(y);
  914.         *e = 32 - k;
  915.         if (k < Ebits) {
  916.                 d0 = Exp_1 | y >> (Ebits - k);
  917.                 w = xa > xa0 ? *--xa : 0;
  918.                 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
  919.                 goto ret_d;
  920.                 }
  921.         z = xa > xa0 ? *--xa : 0;
  922.         if (k -= Ebits) {
  923.                 d0 = Exp_1 | y << k | z >> (32 - k);
  924.                 y = xa > xa0 ? *--xa : 0;
  925.                 d1 = z << k | y >> (32 - k);
  926.                 }
  927.         else {
  928.                 d0 = Exp_1 | y;
  929.                 d1 = z;
  930.                 }
  931.  ret_d:
  932. #ifdef VAX
  933.         word0(d) = d0 >> 16 | d0 << 16;
  934.         word1(d) = d1 >> 16 | d1 << 16;
  935. #else
  936. #undef d0
  937. #undef d1
  938. #endif
  939.         return d;
  940.         }
  941.  
  942. static Bigint *
  943. d2b
  944. #ifdef KR_headers
  945.         (result, d, e, bits) Bigint *result; double d; int *e, *bits;
  946. #else
  947.         (Bigint *result, double d, int *e, int *bits)
  948. #endif
  949. {
  950.         int de, i, k;
  951.         unsigned32 *x, y, z;
  952. #ifdef VAX
  953.         unsigned32 d0, d1;
  954.         d0 = word0(d) >> 16 | word0(d) << 16;
  955.         d1 = word1(d) >> 16 | word1(d) << 16;
  956. #else
  957. #define d0 word0(d)
  958. #define d1 word1(d)
  959. #endif
  960.  
  961.         result = Brealloc(result, 1);
  962.         x = result->x;
  963.  
  964.         z = d0 & Frac_mask;
  965.         d0 &= 0x7fffffff;       /* clear sign bit, which we ignore */
  966.  
  967.         de = (int)(d0 >> Exp_shift);  /* The exponent part of d. */
  968.  
  969.     /* Put back the suppressed high-order bit, if normalized. */
  970. #ifndef IBM
  971. #ifndef Sudden_Underflow
  972.         if (de)
  973. #endif
  974.       z |= Exp_msk11;
  975. #endif
  976.  
  977.         if ((y = d1)) {
  978.                 if ((k = lo0bits(&y))) {
  979.                         x[0] = y | z << (32 - k);
  980.                         z >>= k;
  981.                         }
  982.                 else
  983.                         x[0] = y;
  984.                 i = result->wds = (x[1] = z) ? 2 : 1;
  985.                 }
  986.         else {
  987. #ifdef DEBUG
  988.                 if (!z)
  989.                         Bug("Zero passed to d2b");
  990. #endif
  991.                 k = lo0bits(&z);
  992.                 x[0] = z;
  993.                 i = result->wds = 1;
  994.                 k += 32;
  995.                 }
  996. #ifndef Sudden_Underflow
  997.         if (de) {
  998. #endif
  999. #ifdef IBM
  1000.                 *e = (de - Bias - (P-1) << 2) + k;
  1001.                 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
  1002. #else
  1003.                 *e = de - Bias - (P-1) + k;
  1004.                 *bits = P - k;
  1005. #endif
  1006. #ifndef Sudden_Underflow
  1007.                 }
  1008.         else {
  1009.                 *e = de - Bias - (P-1) + 1 + k;
  1010.                 *bits = 32*i - hi0bits(x[i-1]);
  1011.                 }
  1012. #endif
  1013.         return result;
  1014.         }
  1015. #undef d0
  1016. #undef d1
  1017.  
  1018. static double
  1019. ratio
  1020. #ifdef KR_headers
  1021.         (a, b) Bigint *a, *b;
  1022. #else
  1023.         (Bigint *a, Bigint *b)
  1024. #endif
  1025. {
  1026.         double da, db;
  1027.         int k, ka, kb;
  1028.  
  1029.         da = b2d(a, &ka);
  1030.         db = b2d(b, &kb);
  1031.         k = ka - kb + 32*(a->wds - b->wds);
  1032. #ifdef IBM
  1033.         if (k > 0) {
  1034.                 word0(da) += (k >> 2)*Exp_msk1;
  1035.                 if (k &= 3)
  1036.                         da *= 1 << k;
  1037.                 }
  1038.         else {
  1039.                 k = -k;
  1040.                 word0(db) += (k >> 2)*Exp_msk1;
  1041.                 if (k &= 3)
  1042.                         db *= 1 << k;
  1043.                 }
  1044. #else
  1045.         if (k > 0)
  1046.                 word0(da) += k*Exp_msk1;
  1047.         else {
  1048.                 k = -k;
  1049.                 word0(db) += k*Exp_msk1;
  1050.                 }
  1051. #endif
  1052.         return da / db;
  1053.         }
  1054.  
  1055. static double
  1056. tens[] = {
  1057.                 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1058.                 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1059.                 1e20, 1e21, 1e22
  1060. #ifdef VAX
  1061.                 , 1e23, 1e24
  1062. #endif
  1063.                 };
  1064.  
  1065. static double
  1066. #ifdef IEEE_Arith
  1067. bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1068. static double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
  1069. #define n_bigtens 5
  1070. #else
  1071. #ifdef IBM
  1072. bigtens[] = { 1e16, 1e32, 1e64 };
  1073. static double tinytens[] = { 1e-16, 1e-32, 1e-64 };
  1074. #define n_bigtens 3
  1075. #else
  1076. bigtens[] = { 1e16, 1e32 };
  1077. static double tinytens[] = { 1e-16, 1e-32 };
  1078. #define n_bigtens 2
  1079. #endif
  1080. #endif
  1081.  
  1082.  double
  1083. _IO_strtod
  1084. #ifdef KR_headers
  1085.         (s00, se) CONST char *s00; char **se;
  1086. #else
  1087.         (CONST char *s00, char **se)
  1088. #endif
  1089. {
  1090.         int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
  1091.                  e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
  1092.         CONST char *s, *s0, *s1;
  1093.         double aadj, aadj1, adj, rv, rv0;
  1094.         _G_int32_t L;
  1095.         unsigned32 y, z;
  1096.     Bigint _bb, _b_avail, _bd, _bd0, _bs, _delta;
  1097.     Bigint *bb = Binit(&_bb);
  1098.     Bigint *bd = Binit(&_bd);
  1099.     Bigint *bd0 = Binit(&_bd0);
  1100.     Bigint *bs = Binit(&_bs);
  1101.     Bigint *b_avail = Binit(&_b_avail);
  1102.     Bigint *delta = Binit(&_delta);
  1103.  
  1104.     TEST_ENDIANNESS;
  1105.         sign = nz0 = nz = 0;
  1106.         rv = 0.;
  1107.         for(s = s00;;s++) switch(*s) {
  1108.                 case '-':
  1109.                         sign = 1;
  1110.                         /* no break */
  1111.                 case '+':
  1112.                         if (*++s)
  1113.                                 goto break2;
  1114.                         /* no break */
  1115.                 case 0:
  1116.             /* "+" and "-" should be reported as an error? */
  1117.             sign = 0;
  1118.             s = s00;
  1119.                         goto ret;
  1120.                 case '\t':
  1121.                 case '\n':
  1122.                 case '\v':
  1123.                 case '\f':
  1124.                 case '\r':
  1125.                 case ' ':
  1126.                         continue;
  1127.                 default:
  1128.                         goto break2;
  1129.                 }
  1130.  break2:
  1131.         if (*s == '0') {
  1132.                 nz0 = 1;
  1133.                 while(*++s == '0') ;
  1134.                 if (!*s)
  1135.                         goto ret;
  1136.                 }
  1137.         s0 = s;
  1138.         y = z = 0;
  1139.         for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
  1140.                 if (nd < 9)
  1141.                         y = 10*y + c - '0';
  1142.                 else if (nd < 16)
  1143.                         z = 10*z + c - '0';
  1144.         nd0 = nd;
  1145.         if (c == '.') {
  1146.                 c = *++s;
  1147.                 if (!nd) {
  1148.                         for(; c == '0'; c = *++s)
  1149.                                 nz++;
  1150.                         if (c > '0' && c <= '9') {
  1151.                                 s0 = s;
  1152.                                 nf += nz;
  1153.                                 nz = 0;
  1154.                                 goto have_dig;
  1155.                                 }
  1156.                         goto dig_done;
  1157.                         }
  1158.                 for(; c >= '0' && c <= '9'; c = *++s) {
  1159.  have_dig:
  1160.                         nz++;
  1161.                         if (c -= '0') {
  1162.                                 nf += nz;
  1163.                                 for(i = 1; i < nz; i++)
  1164.                                         if (nd++ < 9)
  1165.                                                 y *= 10;
  1166.                                         else if (nd <= DBL_DIG + 1)
  1167.                                                 z *= 10;
  1168.                                 if (nd++ < 9)
  1169.                                         y = 10*y + c;
  1170.                                 else if (nd <= DBL_DIG + 1)
  1171.                                         z = 10*z + c;
  1172.                                 nz = 0;
  1173.                                 }
  1174.                         }
  1175.                 }
  1176.  dig_done:
  1177.         e = 0;
  1178.         if (c == 'e' || c == 'E') {
  1179.                 if (!nd && !nz && !nz0) {
  1180.                         s = s00;
  1181.                         goto ret;
  1182.                         }
  1183.                 s00 = s;
  1184.                 esign = 0;
  1185.                 switch(c = *++s) {
  1186.                         case '-':
  1187.                                 esign = 1;
  1188.                         case '+':
  1189.                                 c = *++s;
  1190.                         }
  1191.                 if (c >= '0' && c <= '9') {
  1192.                         while(c == '0')
  1193.                                 c = *++s;
  1194.                         if (c > '0' && c <= '9') {
  1195.                                 e = c - '0';
  1196.                                 s1 = s;
  1197.                                 while((c = *++s) >= '0' && c <= '9')
  1198.                                         e = 10*e + c - '0';
  1199.                                 if (s - s1 > 8)
  1200.                                         /* Avoid confusion from exponents
  1201.                                          * so large that e might overflow.
  1202.                                          */
  1203.                                         e = 9999999;
  1204.                                 if (esign)
  1205.                                         e = -e;
  1206.                                 }
  1207.                         else
  1208.                                 e = 0;
  1209.                         }
  1210.                 else
  1211.                         s = s00;
  1212.                 }
  1213.         if (!nd) {
  1214.                 if (!nz && !nz0)
  1215.                         s = s00;
  1216.                 goto ret;
  1217.                 }
  1218.         e1 = e -= nf;
  1219.  
  1220.         /* Now we have nd0 digits, starting at s0, followed by a
  1221.          * decimal point, followed by nd-nd0 digits.  The number we're
  1222.          * after is the integer represented by those digits times
  1223.          * 10**e */
  1224.  
  1225.         if (!nd0)
  1226.                 nd0 = nd;
  1227.         k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  1228.         rv = y;
  1229.         if (k > 9)
  1230.                 rv = tens[k - 9] * rv + z;
  1231.         if (nd <= DBL_DIG
  1232. #ifndef RND_PRODQUOT
  1233.                 && FLT_ROUNDS == 1
  1234. #endif
  1235.                         ) {
  1236.                 if (!e)
  1237.                         goto ret;
  1238.                 if (e > 0) {
  1239.                         if (e <= Ten_pmax) {
  1240. #ifdef VAX
  1241.                                 goto vax_ovfl_check;
  1242. #else
  1243.                                 /* rv = */ rounded_product(rv, tens[e]);
  1244.                                 goto ret;
  1245. #endif
  1246.                                 }
  1247.                         i = DBL_DIG - nd;
  1248.                         if (e <= Ten_pmax + i) {
  1249.                                 /* A fancier test would sometimes let us do
  1250.                                  * this for larger i values.
  1251.                                  */
  1252.                                 e -= i;
  1253.                                 rv *= tens[i];
  1254. #ifdef VAX
  1255.                                 /* VAX exponent range is so narrow we must
  1256.                                  * worry about overflow here...
  1257.                                  */
  1258.  vax_ovfl_check:
  1259.                                 word0(rv) -= P*Exp_msk1;
  1260.                                 /* rv = */ rounded_product(rv, tens[e]);
  1261.                                 if ((word0(rv) & Exp_mask)
  1262.                                  > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
  1263.                                         goto ovfl;
  1264.                                 word0(rv) += P*Exp_msk1;
  1265. #else
  1266.                                 /* rv = */ rounded_product(rv, tens[e]);
  1267. #endif
  1268.                                 goto ret;
  1269.                                 }
  1270.                         }
  1271. #ifndef Inaccurate_Divide
  1272.                 else if (e >= -Ten_pmax) {
  1273.                         /* rv = */ rounded_quotient(rv, tens[-e]);
  1274.                         goto ret;
  1275.                         }
  1276. #endif
  1277.                 }
  1278.         e1 += nd - k;
  1279.  
  1280.         /* Get starting approximation = rv * 10**e1 */
  1281.  
  1282.         if (e1 > 0) {
  1283.                 if ((i = e1 & 15))
  1284.                         rv *= tens[i];
  1285.                 if (e1 &= ~15) {
  1286.                         if (e1 > DBL_MAX_10_EXP) {
  1287.  ovfl:
  1288.                                 errno = ERANGE;
  1289. #if defined(sun) && !defined(__svr4__)
  1290. /* SunOS defines HUGE_VAL as __infinity(), which is in libm. */
  1291. #undef HUGE_VAL
  1292. #endif
  1293. #ifndef HUGE_VAL
  1294. #define HUGE_VAL        1.7976931348623157E+308
  1295. #endif
  1296.                                 rv = HUGE_VAL;
  1297.                                 goto ret;
  1298.                                 }
  1299.                         if (e1 >>= 4) {
  1300.                                 for(j = 0; e1 > 1; j++, e1 >>= 1)
  1301.                                         if (e1 & 1)
  1302.                                                 rv *= bigtens[j];
  1303.                         /* The last multiplication could overflow. */
  1304.                                 word0(rv) -= P*Exp_msk1;
  1305.                                 rv *= bigtens[j];
  1306.                                 if ((z = word0(rv) & Exp_mask)
  1307.                                  > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  1308.                                         goto ovfl;
  1309.                                 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  1310.                                         /* set to largest number */
  1311.                                         /* (Can't trust DBL_MAX) */
  1312.                                         word0(rv) = Big0;
  1313.                                         word1(rv) = Big1;
  1314.                                         }
  1315.                                 else
  1316.                                         word0(rv) += P*Exp_msk1;
  1317.                                 }
  1318.  
  1319.                         }
  1320.                 }
  1321.         else if (e1 < 0) {
  1322.                 e1 = -e1;
  1323.                 if ((i = e1 & 15))
  1324.                         rv /= tens[i];
  1325.                 if (e1 &= ~15) {
  1326.                         e1 >>= 4;
  1327.                         for(j = 0; e1 > 1; j++, e1 >>= 1)
  1328.                                 if (e1 & 1)
  1329.                                         rv *= tinytens[j];
  1330.                         /* The last multiplication could underflow. */
  1331.                         rv0 = rv;
  1332.                         rv *= tinytens[j];
  1333.                         if (!rv) {
  1334.                                 rv = 2.*rv0;
  1335.                                 rv *= tinytens[j];
  1336.                                 if (!rv) {
  1337.  undfl:
  1338.                                         rv = 0.;
  1339.                                         errno = ERANGE;
  1340.                                         goto ret;
  1341.                                         }
  1342.                                 word0(rv) = Tiny0;
  1343.                                 word1(rv) = Tiny1;
  1344.                                 /* The refinement below will clean
  1345.                                  * this approximation up.
  1346.                                  */
  1347.                                 }
  1348.                         }
  1349.                 }
  1350.  
  1351.         /* Now the hard part -- adjusting rv to the correct value.*/
  1352.  
  1353.         /* Put digits into bd: true value = bd * 10^e */
  1354.  
  1355.         bd0 = s2b(bd0, s0, nd0, nd, y);
  1356.     bd = Brealloc(bd, bd0->k);
  1357.  
  1358.         for(;;) {
  1359.                 Bcopy(bd, bd0);
  1360.                 bb = d2b(bb, rv, &bbe, &bbbits);    /* rv = bb * 2^bbe */
  1361.                 bs = i2b(bs, 1);
  1362.  
  1363.                 if (e >= 0) {
  1364.                         bb2 = bb5 = 0;
  1365.                         bd2 = bd5 = e;
  1366.                         }
  1367.                 else {
  1368.                         bb2 = bb5 = -e;
  1369.                         bd2 = bd5 = 0;
  1370.                         }
  1371.                 if (bbe >= 0)
  1372.                         bb2 += bbe;
  1373.                 else
  1374.                         bd2 -= bbe;
  1375.                 bs2 = bb2;
  1376. #ifdef Sudden_Underflow
  1377. #ifdef IBM
  1378.                 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
  1379. #else
  1380.                 j = P + 1 - bbbits;
  1381. #endif
  1382. #else
  1383.                 i = bbe + bbbits - 1;   /* logb(rv) */
  1384.                 if (i < Emin)   /* denormal */
  1385.                         j = bbe + (P-Emin);
  1386.                 else
  1387.                         j = P + 1 - bbbits;
  1388. #endif
  1389.                 bb2 += j;
  1390.                 bd2 += j;
  1391.                 i = bb2 < bd2 ? bb2 : bd2;
  1392.                 if (i > bs2)
  1393.                         i = bs2;
  1394.                 if (i > 0) {
  1395.                         bb2 -= i;
  1396.                         bd2 -= i;
  1397.                         bs2 -= i;
  1398.                         }
  1399.                 if (bb5 > 0) {
  1400.             Bigint *b_tmp;
  1401.                         bs = pow5mult(bs, bb5);
  1402.                         b_tmp = mult(b_avail, bs, bb);
  1403.                         b_avail = bb;
  1404.                         bb = b_tmp;
  1405.                         }
  1406.                 if (bb2 > 0)
  1407.                         bb = lshift(bb, bb2);
  1408.                 if (bd5 > 0)
  1409.                         bd = pow5mult(bd, bd5);
  1410.                 if (bd2 > 0)
  1411.                         bd = lshift(bd, bd2);
  1412.                 if (bs2 > 0)
  1413.                         bs = lshift(bs, bs2);
  1414.                 delta = diff(delta, bb, bd);
  1415.                 dsign = delta->sign;
  1416.                 delta->sign = 0;
  1417.                 i = cmp(delta, bs);
  1418.                 if (i < 0) {
  1419.                         /* Error is less than half an ulp -- check for
  1420.                          * special case of mantissa a power of two.
  1421.                          */
  1422.                         if (dsign || word1(rv) || word0(rv) & Bndry_mask)
  1423.                                 break;
  1424.                         delta = lshift(delta,Log2P);
  1425.                         if (cmp(delta, bs) > 0)
  1426.                                 goto drop_down;
  1427.                         break;
  1428.                         }
  1429.                 if (i == 0) {
  1430.                         /* exactly half-way between */
  1431.                         if (dsign) {
  1432.                                 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
  1433.                                  &&  word1(rv) == 0xffffffff) {
  1434.                                         /*boundary case -- increment exponent*/
  1435.                                         word0(rv) = (word0(rv) & Exp_mask)
  1436.                                                 + Exp_msk1
  1437. #ifdef IBM
  1438.                                                 | Exp_msk1 >> 4
  1439. #endif
  1440.                                                 ;
  1441.                                         word1(rv) = 0;
  1442.                                         break;
  1443.                                         }
  1444.                                 }
  1445.                         else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
  1446.  drop_down:
  1447.                                 /* boundary case -- decrement exponent */
  1448. #ifdef Sudden_Underflow
  1449.                                 L = word0(rv) & Exp_mask;
  1450. #ifdef IBM
  1451.                                 if (L <  Exp_msk1)
  1452. #else
  1453.                                 if (L <= Exp_msk1)
  1454. #endif
  1455.                                         goto undfl;
  1456.                                 L -= Exp_msk1;
  1457. #else
  1458.                                 L = (word0(rv) & Exp_mask) - Exp_msk1;
  1459. #endif
  1460.                                 word0(rv) = L | Bndry_mask1;
  1461.                                 word1(rv) = 0xffffffff;
  1462. #ifdef IBM
  1463.                                 continue;
  1464. #else
  1465.                                 break;
  1466. #endif
  1467.                                 }
  1468. #ifndef ROUND_BIASED
  1469.                         if (!(word1(rv) & LSB))
  1470.                                 break;
  1471. #endif
  1472.                         if (dsign)
  1473.                                 rv += ulp(rv);
  1474. #ifndef ROUND_BIASED
  1475.                         else {
  1476.                                 rv -= ulp(rv);
  1477. #ifndef Sudden_Underflow
  1478.                                 if (!rv)
  1479.                                         goto undfl;
  1480. #endif
  1481.                                 }
  1482. #endif
  1483.                         break;
  1484.                         }
  1485.                 if ((aadj = ratio(delta, bs)) <= 2.) {
  1486.                         if (dsign)
  1487.                                 aadj = aadj1 = 1.;
  1488.                         else if (word1(rv) || word0(rv) & Bndry_mask) {
  1489. #ifndef Sudden_Underflow
  1490.                                 if (word1(rv) == Tiny1 && !word0(rv))
  1491.                                         goto undfl;
  1492. #endif
  1493.                                 aadj = 1.;
  1494.                                 aadj1 = -1.;
  1495.                                 }
  1496.                         else {
  1497.                                 /* special case -- power of FLT_RADIX to be */
  1498.                                 /* rounded down... */
  1499.  
  1500.                                 if (aadj < 2./FLT_RADIX)
  1501.                                         aadj = 1./FLT_RADIX;
  1502.                                 else
  1503.                                         aadj *= 0.5;
  1504.                                 aadj1 = -aadj;
  1505.                                 }
  1506.                         }
  1507.                 else {
  1508.                         aadj *= 0.5;
  1509.                         aadj1 = dsign ? aadj : -aadj;
  1510. #ifdef Check_FLT_ROUNDS
  1511.                         switch(FLT_ROUNDS) {
  1512.                                 case 2: /* towards +infinity */
  1513.                                         aadj1 -= 0.5;
  1514.                                         break;
  1515.                                 case 0: /* towards 0 */
  1516.                                 case 3: /* towards -infinity */
  1517.                                         aadj1 += 0.5;
  1518.                                 }
  1519. #else
  1520.                         if (FLT_ROUNDS == 0)
  1521.                                 aadj1 += 0.5;
  1522. #endif
  1523.                         }
  1524.                 y = word0(rv) & Exp_mask;
  1525.  
  1526.                 /* Check for overflow */
  1527.  
  1528.                 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
  1529.                         rv0 = rv;
  1530.                         word0(rv) -= P*Exp_msk1;
  1531.                         adj = aadj1 * ulp(rv);
  1532.                         rv += adj;
  1533.                         if ((word0(rv) & Exp_mask) >=
  1534.                                         Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
  1535.                                 if (word0(rv0) == Big0 && word1(rv0) == Big1)
  1536.                                         goto ovfl;
  1537.                                 word0(rv) = Big0;
  1538.                                 word1(rv) = Big1;
  1539.                                 continue;
  1540.                                 }
  1541.                         else
  1542.                                 word0(rv) += P*Exp_msk1;
  1543.                         }
  1544.                 else {
  1545. #ifdef Sudden_Underflow
  1546.                         if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
  1547.                                 rv0 = rv;
  1548.                                 word0(rv) += P*Exp_msk1;
  1549.                                 adj = aadj1 * ulp(rv);
  1550.                                 rv += adj;
  1551. #ifdef IBM
  1552.                                 if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
  1553. #else
  1554.                                 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
  1555. #endif
  1556.                                         {
  1557.                                         if (word0(rv0) == Tiny0
  1558.                                          && word1(rv0) == Tiny1)
  1559.                                                 goto undfl;
  1560.                                         word0(rv) = Tiny0;
  1561.                                         word1(rv) = Tiny1;
  1562.                                         continue;
  1563.                                         }
  1564.                                 else
  1565.                                         word0(rv) -= P*Exp_msk1;
  1566.                                 }
  1567.                         else {
  1568.                                 adj = aadj1 * ulp(rv);
  1569.                                 rv += adj;
  1570.                                 }
  1571. #else
  1572.                         /* Compute adj so that the IEEE rounding rules will
  1573.                          * correctly round rv + adj in some half-way cases.
  1574.                          * If rv * ulp(rv) is denormalized (i.e.,
  1575.                          * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
  1576.                          * trouble from bits lost to denormalization;
  1577.                          * example: 1.2e-307 .
  1578.                          */
  1579.                         if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
  1580.                                 aadj1 = (double)(int)(aadj + 0.5);
  1581.                                 if (!dsign)
  1582.                                         aadj1 = -aadj1;
  1583.                                 }
  1584.                         adj = aadj1 * ulp(rv);
  1585.                         rv += adj;
  1586. #endif
  1587.                         }
  1588.                 z = word0(rv) & Exp_mask;
  1589.                 if (y == z) {
  1590.                         /* Can we stop now? */
  1591.                         L = (_G_int32_t)aadj;
  1592.                         aadj -= L;
  1593.                         /* The tolerances below are conservative. */
  1594.                         if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
  1595.                                 if (aadj < .4999999 || aadj > .5000001)
  1596.                                         break;
  1597.                                 }
  1598.                         else if (aadj < .4999999/FLT_RADIX)
  1599.                                 break;
  1600.                         }
  1601.                 }
  1602.         Bfree(bb);
  1603.         Bfree(bd);
  1604.         Bfree(bs);
  1605.         Bfree(bd0);
  1606.         Bfree(delta);
  1607.     Bfree(b_avail);
  1608.  ret:
  1609.         if (se)
  1610.                 *se = (char *)s;
  1611.         return sign ? -rv : rv;
  1612.         }
  1613.  
  1614. static int
  1615. quorem
  1616. #ifdef KR_headers
  1617.         (b, S) Bigint *b, *S;
  1618. #else
  1619.         (Bigint *b, Bigint *S)
  1620. #endif
  1621. {
  1622.         int n;
  1623.         _G_int32_t borrow, y;
  1624.         unsigned32 carry, q, ys;
  1625.         unsigned32 *bx, *bxe, *sx, *sxe;
  1626.         _G_int32_t z;
  1627.         unsigned32 si, zs;
  1628.  
  1629.         n = S->wds;
  1630. #ifdef DEBUG
  1631.         /*debug*/ if (b->wds > n)
  1632.         /*debug*/       Bug("oversize b in quorem");
  1633. #endif
  1634.         if (b->wds < n)
  1635.                 return 0;
  1636.         sx = S->x;
  1637.         sxe = sx + --n;
  1638.         bx = b->x;
  1639.         bxe = bx + n;
  1640.         q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
  1641. #ifdef DEBUG
  1642.         /*debug*/ if (q > 9)
  1643.         /*debug*/       Bug("oversized quotient in quorem");
  1644. #endif
  1645.         if (q) {
  1646.                 borrow = 0;
  1647.                 carry = 0;
  1648.                 do {
  1649.                         si = *sx++;
  1650.                         ys = (si & 0xffff) * q + carry;
  1651.                         zs = (si >> 16) * q + (ys >> 16);
  1652.                         carry = zs >> 16;
  1653.                         y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  1654.                         borrow = y >> 16;
  1655.                         Sign_Extend(borrow, y);
  1656.                         z = (*bx >> 16) - (zs & 0xffff) + borrow;
  1657.                         borrow = z >> 16;
  1658.                         Sign_Extend(borrow, z);
  1659.                         Storeinc(bx, z, y);
  1660.                         }
  1661.                         while(sx <= sxe);
  1662.                 if (!*bxe) {
  1663.                         bx = b->x;
  1664.                         while(--bxe > bx && !*bxe)
  1665.                                 --n;
  1666.                         b->wds = n;
  1667.                         }
  1668.                 }
  1669.         if (cmp(b, S) >= 0) {
  1670.                 q++;
  1671.                 borrow = 0;
  1672.                 carry = 0;
  1673.                 bx = b->x;
  1674.                 sx = S->x;
  1675.                 do {
  1676.                         si = *sx++;
  1677.                         ys = (si & 0xffff) + carry;
  1678.                         zs = (si >> 16) + (ys >> 16);
  1679.                         carry = zs >> 16;
  1680.                         y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  1681.                         borrow = y >> 16;
  1682.                         Sign_Extend(borrow, y);
  1683.                         z = (*bx >> 16) - (zs & 0xffff) + borrow;
  1684.                         borrow = z >> 16;
  1685.                         Sign_Extend(borrow, z);
  1686.                         Storeinc(bx, z, y);
  1687.                         }
  1688.                         while(sx <= sxe);
  1689.                 bx = b->x;
  1690.                 bxe = bx + n;
  1691.                 if (!*bxe) {
  1692.                         while(--bxe > bx && !*bxe)
  1693.                                 --n;
  1694.                         b->wds = n;
  1695.                         }
  1696.                 }
  1697.         return q;
  1698.         }
  1699.  
  1700. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  1701.  *
  1702.  * Inspired by "How to Print Floating-Point Numbers Accurately" by
  1703.  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  1704.  *
  1705.  * Modifications:
  1706.  *      1. Rather than iterating, we use a simple numeric overestimate
  1707.  *         to determine k = floor(log10(d)).  We scale relevant
  1708.  *         quantities using O(log2(k)) rather than O(k) multiplications.
  1709.  *      2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  1710.  *         try to generate digits strictly left to right.  Instead, we
  1711.  *         compute with fewer bits and propagate the carry if necessary
  1712.  *         when rounding the final digit up.  This is often faster.
  1713.  *      3. Under the assumption that input will be rounded nearest,
  1714.  *         mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  1715.  *         That is, we allow equality in stopping tests when the
  1716.  *         round-nearest rule will give the same floating-point value
  1717.  *         as would satisfaction of the stopping test with strict
  1718.  *         inequality.
  1719.  *      4. We remove common factors of powers of 2 from relevant
  1720.  *         quantities.
  1721.  *      5. When converting floating-point integers less than 1e16,
  1722.  *         we use floating-point arithmetic rather than resorting
  1723.  *         to multiple-precision integers.
  1724.  *      6. When asked to produce fewer than 15 digits, we first try
  1725.  *         to get by with floating-point arithmetic; we resort to
  1726.  *         multiple-precision integer arithmetic only if we cannot
  1727.  *         guarantee that the floating-point calculation has given
  1728.  *         the correctly rounded result.  For k requested digits and
  1729.  *         "uniformly" distributed input, the probability is
  1730.  *         something like 10^(k-15) that we must resort to the long
  1731.  *         calculation.
  1732.  */
  1733.  
  1734.  char *
  1735. _IO_dtoa
  1736. #ifdef KR_headers
  1737.         (d, mode, ndigits, decpt, sign, rve)
  1738.         double d; int mode, ndigits, *decpt, *sign; char **rve;
  1739. #else
  1740.         (double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
  1741. #endif
  1742. {
  1743.  /*     Arguments ndigits, decpt, sign are similar to those
  1744.         of ecvt and fcvt; trailing zeros are suppressed from
  1745.         the returned string.  If not null, *rve is set to point
  1746.         to the end of the return value.  If d is +-Infinity or NaN,
  1747.         then *decpt is set to 9999.
  1748.  
  1749.         mode:
  1750.                 0 ==> shortest string that yields d when read in
  1751.                         and rounded to nearest.
  1752.                 1 ==> like 0, but with Steele & White stopping rule;
  1753.                         e.g. with IEEE P754 arithmetic , mode 0 gives
  1754.                         1e23 whereas mode 1 gives 9.999999999999999e22.
  1755.                 2 ==> max(1,ndigits) significant digits.  This gives a
  1756.                         return value similar to that of ecvt, except
  1757.                         that trailing zeros are suppressed.
  1758.                 3 ==> through ndigits past the decimal point.  This
  1759.                         gives a return value similar to that from fcvt,
  1760.                         except that trailing zeros are suppressed, and
  1761.                         ndigits can be negative.
  1762.                 4-9 should give the same return values as 2-3, i.e.,
  1763.                         4 <= mode <= 9 ==> same return as mode
  1764.                         2 + (mode & 1).  These modes are mainly for
  1765.                         debugging; often they run slower but sometimes
  1766.                         faster than modes 2-3.
  1767.                 4,5,8,9 ==> left-to-right digit generation.
  1768.                 6-9 ==> don't try fast floating-point estimate
  1769.                         (if applicable).
  1770.  
  1771.                 Values of mode other than 0-9 are treated as mode 0.
  1772.  
  1773.                 Sufficient space is allocated to the return value
  1774.                 to hold the suppressed trailing zeros.
  1775.         */
  1776.  
  1777.         int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  1778.                 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  1779.                 spec_case, try_quick;
  1780.         _G_int32_t L;
  1781. #ifndef Sudden_Underflow
  1782.         int denorm;
  1783. #endif
  1784.     Bigint _b_avail, _b, _mhi, _mlo, _S;
  1785.     Bigint *b_avail = Binit(&_b_avail);
  1786.     Bigint *b = Binit(&_b);
  1787.     Bigint *S = Binit(&_S);
  1788.     /* mhi and mlo are only set and used if leftright. */
  1789.         Bigint *mhi = NULL, *mlo = NULL;
  1790.         double d2, ds, eps;
  1791.         char *s, *s0;
  1792.         static Bigint *result = NULL;
  1793.         static int result_k;
  1794.  
  1795.     TEST_ENDIANNESS;
  1796.         if (result) {
  1797.         /* result is contains a string, so its fields (interpreted
  1798.            as a Bigint have been trashed.  Restore them.
  1799.            This is a really ugly interface - result should
  1800.            not be static, since that is not thread-safe.  FIXME. */
  1801.                 result->k = result_k;
  1802.                 result->maxwds = 1 << result_k;
  1803.                 result->on_stack = 0;
  1804.                 }
  1805.  
  1806.         if (word0(d) & Sign_bit) {
  1807.                 /* set sign for everything, including 0's and NaNs */
  1808.                 *sign = 1;
  1809.                 word0(d) &= ~Sign_bit;  /* clear sign bit */
  1810.                 }
  1811.         else
  1812.                 *sign = 0;
  1813.  
  1814. #if defined(IEEE_Arith) + defined(VAX)
  1815. #ifdef IEEE_Arith
  1816.         if ((word0(d) & Exp_mask) == Exp_mask)
  1817. #else
  1818.         if (word0(d)  == 0x8000)
  1819. #endif
  1820.                 {
  1821.                 /* Infinity or NaN */
  1822.                 *decpt = 9999;
  1823. #ifdef IEEE_Arith
  1824.         if (!word1(d) && !(word0(d) & 0xfffff))
  1825.           {
  1826.             s = "Infinity";
  1827.             if (rve)
  1828.               *rve = s + 8;
  1829.           }
  1830.         else
  1831. #endif
  1832.           {
  1833.             s = "NaN";
  1834.             if (rve)
  1835.               *rve = s +3;
  1836.           }
  1837.                 return s;
  1838.                 }
  1839. #endif
  1840. #ifdef IBM
  1841.         d += 0; /* normalize */
  1842. #endif
  1843.         if (!d) {
  1844.                 *decpt = 1;
  1845.                 s = "0";
  1846.                 if (rve)
  1847.                         *rve = s + 1;
  1848.                 return s;
  1849.                 }
  1850.  
  1851.         b = d2b(b, d, &be, &bbits);
  1852.         i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
  1853. #ifndef Sudden_Underflow
  1854.         if (i) {
  1855. #endif
  1856.                 d2 = d;
  1857.                 word0(d2) &= Frac_mask1;
  1858.                 word0(d2) |= Exp_11;
  1859. #ifdef IBM
  1860.                 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
  1861.                         d2 /= 1 << j;
  1862. #endif
  1863.  
  1864.                 i -= Bias;
  1865. #ifdef IBM
  1866.                 i <<= 2;
  1867.                 i += j;
  1868. #endif
  1869. #ifndef Sudden_Underflow
  1870.                 denorm = 0;
  1871.                 }
  1872.         else {
  1873.                 /* d is denormalized */
  1874.         unsigned32 x;
  1875.  
  1876.                 i = bbits + be + (Bias + (P-1) - 1);
  1877.                 x = i > 32  ? word0(d) << (64 - i) | word1(d) >> (i - 32)
  1878.                             : word1(d) << (32 - i);
  1879.                 d2 = x;
  1880.                 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
  1881.                 i -= (Bias + (P-1) - 1) + 1;
  1882.                 denorm = 1;
  1883.                 }
  1884. #endif
  1885.  
  1886.     /* Now i is the unbiased base-2 exponent. */
  1887.  
  1888.         /* log(x)       ~=~ log(1.5) + (x-1.5)/1.5
  1889.          * log10(x)      =  log(x) / log(10)
  1890.          *              ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  1891.          * log10(d) = i*log(2)/log(10) + log10(d2)
  1892.          *
  1893.          * This suggests computing an approximation k to log10(d) by
  1894.          *
  1895.          * k = i*0.301029995663981
  1896.          *      + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  1897.          *
  1898.          * We want k to be too large rather than too small.
  1899.          * The error in the first-order Taylor series approximation
  1900.          * is in our favor, so we just round up the constant enough
  1901.          * to compensate for any error in the multiplication of
  1902.          * (i) by 0.301029995663981; since |i| <= 1077,
  1903.          * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  1904.          * adding 1e-13 to the constant term more than suffices.
  1905.          * Hence we adjust the constant term to 0.1760912590558.
  1906.          * (We could get a more accurate k by invoking log10,
  1907.          *  but this is probably not worthwhile.)
  1908.          */
  1909.  
  1910.         ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  1911.         k = (int)ds;
  1912.         if (ds < 0. && ds != k)
  1913.                 k--;    /* want k = floor(ds) */
  1914.         k_check = 1;
  1915.         if (k >= 0 && k <= Ten_pmax) {
  1916.                 if (d < tens[k])
  1917.                         k--;
  1918.                 k_check = 0;
  1919.                 }
  1920.         j = bbits - i - 1;
  1921.         if (j >= 0) {
  1922.                 b2 = 0;
  1923.                 s2 = j;
  1924.                 }
  1925.         else {
  1926.                 b2 = -j;
  1927.                 s2 = 0;
  1928.                 }
  1929.         if (k >= 0) {
  1930.                 b5 = 0;
  1931.                 s5 = k;
  1932.                 s2 += k;
  1933.                 }
  1934.         else {
  1935.                 b2 -= k;
  1936.                 b5 = -k;
  1937.                 s5 = 0;
  1938.                 }
  1939.         if (mode < 0 || mode > 9)
  1940.                 mode = 0;
  1941.         try_quick = 1;
  1942.         if (mode > 5) {
  1943.                 mode -= 4;
  1944.                 try_quick = 0;
  1945.                 }
  1946.         leftright = 1;
  1947.         switch(mode) {
  1948.                 case 0:
  1949.                 case 1:
  1950.                         ilim = ilim1 = -1;
  1951.                         i = 18;
  1952.                         ndigits = 0;
  1953.                         break;
  1954.                 case 2:
  1955.                         leftright = 0;
  1956.                         /* no break */
  1957.                 case 4:
  1958.                         if (ndigits <= 0)
  1959.                                 ndigits = 1;
  1960.                         ilim = ilim1 = i = ndigits;
  1961.                         break;
  1962.                 case 3:
  1963.                         leftright = 0;
  1964.                         /* no break */
  1965.                 case 5:
  1966.                         i = ndigits + k + 1;
  1967.                         ilim = i;
  1968.                         ilim1 = i - 1;
  1969.                         if (i <= 0)
  1970.                                 i = 1;
  1971.                 }
  1972.     /* i is now an upper bound of the number of digits to generate. */
  1973.         j = sizeof(unsigned32) * (1<<BIGINT_MINIMUM_K);
  1974.     /* The test is <= so as to allow room for the final '\0'. */
  1975.         for(result_k = BIGINT_MINIMUM_K; BIGINT_HEADER_SIZE + j <= i;
  1976.                 j <<= 1) result_k++;
  1977.         if (!result || result_k > result->k)
  1978.         {
  1979.           Bfree (result);
  1980.           result = Balloc(result_k);
  1981.         }
  1982.         s = s0 = (char *)result;
  1983.  
  1984.         if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  1985.  
  1986.                 /* Try to get by with floating-point arithmetic. */
  1987.  
  1988.                 i = 0;
  1989.                 d2 = d;
  1990.                 k0 = k;
  1991.                 ilim0 = ilim;
  1992.                 ieps = 2; /* conservative */
  1993.                 if (k > 0) {
  1994.                         ds = tens[k&0xf];
  1995.                         j = k >> 4;
  1996.                         if (j & Bletch) {
  1997.                                 /* prevent overflows */
  1998.                                 j &= Bletch - 1;
  1999.                                 d /= bigtens[n_bigtens-1];
  2000.                                 ieps++;
  2001.                                 }
  2002.                         for(; j; j >>= 1, i++)
  2003.                                 if (j & 1) {
  2004.                                         ieps++;
  2005.                                         ds *= bigtens[i];
  2006.                                         }
  2007.                         d /= ds;
  2008.                         }
  2009.                 else if ((j1 = -k)) {
  2010.                         d *= tens[j1 & 0xf];
  2011.                         for(j = j1 >> 4; j; j >>= 1, i++)
  2012.                                 if (j & 1) {
  2013.                                         ieps++;
  2014.                                         d *= bigtens[i];
  2015.                                         }
  2016.                         }
  2017.                 if (k_check && d < 1. && ilim > 0) {
  2018.                         if (ilim1 <= 0)
  2019.                                 goto fast_failed;
  2020.                         ilim = ilim1;
  2021.                         k--;
  2022.                         d *= 10.;
  2023.                         ieps++;
  2024.                         }
  2025.                 eps = ieps*d + 7.;
  2026.                 word0(eps) -= (P-1)*Exp_msk1;
  2027.                 if (ilim == 0) {
  2028.                         d -= 5.;
  2029.                         if (d > eps)
  2030.                                 goto one_digit;
  2031.                         if (d < -eps)
  2032.                                 goto no_digits;
  2033.                         goto fast_failed;
  2034.                         }
  2035. #ifndef No_leftright
  2036.                 if (leftright) {
  2037.                         /* Use Steele & White method of only
  2038.                          * generating digits needed.
  2039.                          */
  2040.                         eps = 0.5/tens[ilim-1] - eps;
  2041.                         for(i = 0;;) {
  2042.                                 L = (_G_int32_t)d;
  2043.                                 d -= L;
  2044.                                 *s++ = '0' + (int)L;
  2045.                                 if (d < eps)
  2046.                                         goto ret1;
  2047.                                 if (1. - d < eps)
  2048.                                         goto bump_up;
  2049.                                 if (++i >= ilim)
  2050.                                         break;
  2051.                                 eps *= 10.;
  2052.                                 d *= 10.;
  2053.                                 }
  2054.                         }
  2055.                 else {
  2056. #endif
  2057.                         /* Generate ilim digits, then fix them up. */
  2058.                         eps *= tens[ilim-1];
  2059.                         for(i = 1;; i++, d *= 10.) {
  2060.                                 L = (_G_int32_t)d;
  2061.                                 d -= L;
  2062.                                 *s++ = '0' + (int)L;
  2063.                                 if (i == ilim) {
  2064.                                         if (d > 0.5 + eps)
  2065.                                                 goto bump_up;
  2066.                                         else if (d < 0.5 - eps) {
  2067.                                                 while(*--s == '0');
  2068.                                                 s++;
  2069.                                                 goto ret1;
  2070.                                                 }
  2071.                                         break;
  2072.                                         }
  2073.                                 }
  2074. #ifndef No_leftright
  2075.                         }
  2076. #endif
  2077.  fast_failed:
  2078.                 s = s0;
  2079.                 d = d2;
  2080.                 k = k0;
  2081.                 ilim = ilim0;
  2082.                 }
  2083.  
  2084.         /* Do we have a "small" integer? */
  2085.  
  2086.         if (be >= 0 && k <= Int_max) {
  2087.                 /* Yes. */
  2088.                 ds = tens[k];
  2089.                 if (ndigits < 0 && ilim <= 0) {
  2090.                         if (ilim < 0 || d <= 5*ds)
  2091.                                 goto no_digits;
  2092.                         goto one_digit;
  2093.                         }
  2094.                 for(i = 1;; i++) {
  2095.                         L = (_G_int32_t)(d / ds);
  2096.                         d -= L*ds;
  2097. #ifdef Check_FLT_ROUNDS
  2098.                         /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  2099.                         if (d < 0) {
  2100.                                 L--;
  2101.                                 d += ds;
  2102.                                 }
  2103. #endif
  2104.                         *s++ = '0' + (int)L;
  2105.                         if (i == ilim) {
  2106.                                 d += d;
  2107.                                 if (d > ds || (d == ds && L & 1)) {
  2108.  bump_up:
  2109.                                         while(*--s == '9')
  2110.                                                 if (s == s0) {
  2111.                                                         k++;
  2112.                                                         *s = '0';
  2113.                                                         break;
  2114.                                                         }
  2115.                                         ++*s++;
  2116.                                         }
  2117.                                 break;
  2118.                                 }
  2119.                         if (!(d *= 10.))
  2120.                                 break;
  2121.                         }
  2122.                 goto ret1;
  2123.                 }
  2124.  
  2125.         m2 = b2;
  2126.         m5 = b5;
  2127.         if (leftright) {
  2128.                 if (mode < 2) {
  2129.                         i =
  2130. #ifndef Sudden_Underflow
  2131.                                 denorm ? be + (Bias + (P-1) - 1 + 1) :
  2132. #endif
  2133. #ifdef IBM
  2134.                                 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
  2135. #else
  2136.                                 1 + P - bbits;
  2137. #endif
  2138.                         }
  2139.                 else {
  2140.                         j = ilim - 1;
  2141.                         if (m5 >= j)
  2142.                                 m5 -= j;
  2143.                         else {
  2144.                                 s5 += j -= m5;
  2145.                                 b5 += j;
  2146.                                 m5 = 0;
  2147.                                 }
  2148.                         if ((i = ilim) < 0) {
  2149.                                 m2 -= i;
  2150.                                 i = 0;
  2151.                                 }
  2152.                         }
  2153.                 b2 += i;
  2154.                 s2 += i;
  2155.                 mhi = i2b(Binit(&_mhi), 1);
  2156.                 }
  2157.         if (m2 > 0 && s2 > 0) {
  2158.                 i = m2 < s2 ? m2 : s2;
  2159.                 b2 -= i;
  2160.                 m2 -= i;
  2161.                 s2 -= i;
  2162.                 }
  2163.         if (b5 > 0) {
  2164.                 if (leftright) {
  2165.                         if (m5 > 0) {
  2166.                 Bigint *b_tmp;
  2167.                                 mhi = pow5mult(mhi, m5);
  2168.                                 b_tmp = mult(b_avail, mhi, b);
  2169.                                 b_avail = b;
  2170.                                 b = b_tmp;
  2171.                                 }
  2172.                         if ((j = b5 - m5))
  2173.                                 b = pow5mult(b, j);
  2174.                         }
  2175.                 else
  2176.                         b = pow5mult(b, b5);
  2177.                 }
  2178.         S = i2b(S, 1);
  2179.         if (s5 > 0)
  2180.                 S = pow5mult(S, s5);
  2181.  
  2182.         /* Check for special case that d is a normalized power of 2. */
  2183.  
  2184.         if (mode < 2) {
  2185.                 if (!word1(d) && !(word0(d) & Bndry_mask)
  2186. #ifndef Sudden_Underflow
  2187.                  && word0(d) & Exp_mask
  2188. #endif
  2189.                                 ) {
  2190.                         /* The special case */
  2191.                         b2 += Log2P;
  2192.                         s2 += Log2P;
  2193.                         spec_case = 1;
  2194.                         }
  2195.                 else
  2196.                         spec_case = 0;
  2197.                 }
  2198.  
  2199.         /* Arrange for convenient computation of quotients:
  2200.          * shift left if necessary so divisor has 4 leading 0 bits.
  2201.          *
  2202.          * Perhaps we should just compute leading 28 bits of S once
  2203.          * and for all and pass them and a shift to quorem, so it
  2204.          * can do shifts and ors to compute the numerator for q.
  2205.          */
  2206.         if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
  2207.                 i = 32 - i;
  2208.         if (i > 4) {
  2209.                 i -= 4;
  2210.                 b2 += i;
  2211.                 m2 += i;
  2212.                 s2 += i;
  2213.                 }
  2214.         else if (i < 4) {
  2215.                 i += 28;
  2216.                 b2 += i;
  2217.                 m2 += i;
  2218.                 s2 += i;
  2219.                 }
  2220.         if (b2 > 0)
  2221.                 b = lshift(b, b2);
  2222.         if (s2 > 0)
  2223.                 S = lshift(S, s2);
  2224.         if (k_check) {
  2225.                 if (cmp(b,S) < 0) {
  2226.                         k--;
  2227.                         b = multadd(b, 10, 0);  /* we botched the k estimate */
  2228.                         if (leftright)
  2229.                                 mhi = multadd(mhi, 10, 0);
  2230.                         ilim = ilim1;
  2231.                         }
  2232.                 }
  2233.         if (ilim <= 0 && mode > 2) {
  2234.                 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  2235.                         /* no digits, fcvt style */
  2236.  no_digits:
  2237.                         k = -1 - ndigits;
  2238.                         goto ret;
  2239.                         }
  2240.  one_digit:
  2241.                 *s++ = '1';
  2242.                 k++;
  2243.                 goto ret;
  2244.                 }
  2245.         if (leftright) {
  2246.                 if (m2 > 0)
  2247.                         mhi = lshift(mhi, m2);
  2248.  
  2249.                 /* Compute mlo -- check for special case
  2250.                  * that d is a normalized power of 2.
  2251.                  */
  2252.  
  2253.                 if (spec_case) {
  2254.             mlo = Brealloc(Binit(&_mlo), mhi->k);
  2255.                         Bcopy(mlo, mhi);
  2256.                         mhi = lshift(mhi, Log2P);
  2257.                         }
  2258.         else
  2259.             mlo = mhi;
  2260.  
  2261.                 for(i = 1;;i++) {
  2262.                         dig = quorem(b,S) + '0';
  2263.                         /* Do we yet have the shortest decimal string
  2264.                          * that will round to d?
  2265.                          */
  2266.                         j = cmp(b, mlo);
  2267.                         b_avail = diff(b_avail, S, mhi); /* b_avail = S - mi */
  2268.                         j1 = b_avail->sign ? 1 : cmp(b, b_avail);
  2269. #ifndef ROUND_BIASED
  2270.                         if (j1 == 0 && !mode && !(word1(d) & 1)) {
  2271.                                 if (dig == '9')
  2272.                                         goto round_9_up;
  2273.                                 if (j > 0)
  2274.                                         dig++;
  2275.                                 *s++ = dig;
  2276.                                 goto ret;
  2277.                                 }
  2278. #endif
  2279.                         if (j < 0 || (j == 0 && !mode
  2280. #ifndef ROUND_BIASED
  2281.                                                         && !(word1(d) & 1)
  2282. #endif
  2283.                                         )) {
  2284.                                 if (j1 > 0) {
  2285.                                         b = lshift(b, 1);
  2286.                                         j1 = cmp(b, S);
  2287.                                         if ((j1 > 0 || (j1 == 0 && dig & 1))
  2288.                                         && dig++ == '9')
  2289.                                                 goto round_9_up;
  2290.                                         }
  2291.                                 *s++ = dig;
  2292.                                 goto ret;
  2293.                                 }
  2294.                         if (j1 > 0) {
  2295.                                 if (dig == '9') { /* possible if i == 1 */
  2296.  round_9_up:
  2297.                                         *s++ = '9';
  2298.                                         goto roundoff;
  2299.                                         }
  2300.                                 *s++ = dig + 1;
  2301.                                 goto ret;
  2302.                                 }
  2303.                         *s++ = dig;
  2304.                         if (i == ilim)
  2305.                                 break;
  2306.                         b = multadd(b, 10, 0);
  2307.                         if (mlo == mhi)
  2308.                                 mlo = mhi = multadd(mhi, 10, 0);
  2309.                         else {
  2310.                                 mlo = multadd(mlo, 10, 0);
  2311.                                 mhi = multadd(mhi, 10, 0);
  2312.                                 }
  2313.                         }
  2314.                 }
  2315.         else
  2316.                 for(i = 1;; i++) {
  2317.                         *s++ = dig = quorem(b,S) + '0';
  2318.                         if (i >= ilim)
  2319.                                 break;
  2320.                         b = multadd(b, 10, 0);
  2321.                         }
  2322.  
  2323.         /* Round off last digit */
  2324.  
  2325.         b = lshift(b, 1);
  2326.         j = cmp(b, S);
  2327.         if (j > 0 || (j == 0 && dig & 1)) {
  2328.  roundoff:
  2329.                 while(*--s == '9')
  2330.                         if (s == s0) {
  2331.                                 k++;
  2332.                                 *s++ = '1';
  2333.                                 goto ret;
  2334.                                 }
  2335.                 ++*s++;
  2336.                 }
  2337.         else {
  2338.                 while(*--s == '0');
  2339.                 s++;
  2340.                 }
  2341.  ret:
  2342.     Bfree(b_avail);
  2343.         Bfree(S);
  2344.         if (mhi) {
  2345.                 if (mlo && mlo != mhi)
  2346.                         Bfree(mlo);
  2347.                 Bfree(mhi);
  2348.                 }
  2349.  ret1:
  2350.         Bfree(b);
  2351.         *s = 0;
  2352.         *decpt = k + 1;
  2353.         if (rve)
  2354.                 *rve = s;
  2355.         return s0;
  2356.         }
  2357. #endif /* _IO_USE_DTOA */
  2358.